package com.molyfun.parents.activity.schoolonline.homework.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.attachment.AttachmentViewer;
import com.molyfun.parents.activity.schoolonline.homework.HomeWorkDetailActivity;
import com.molyfun.parents.rest.bean.HomeWorkResponse;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class HomeWorkView extends LinearLayout implements View.OnClickListener {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.homework_name)
    TextView mHomeWorkName;
    @InjectView(R.id.read_state)
    ImageView mReadState;
    @InjectView(R.id.attachment_viewer)
    AttachmentViewer mAttachmentViewer;
    @InjectView(R.id.home_work_attachment)
    TextView mHomeWorkTitle;
    @InjectView(R.id.line)
    View mLine;

    private HomeWorkResponse.HomeWorkBean mHomeWorkBean;

    public HomeWorkView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public HomeWorkView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public HomeWorkView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_homework, this);
        ButterKnife.inject(this);
        mHomeWorkName.setOnClickListener(this);
        mReadState.setOnClickListener(this);
        mHomeWorkTitle.setOnClickListener(this);
    }

    public void initData(HomeWorkResponse.HomeWorkBean homeWorkBean) {
        initData(homeWorkBean, false);
    }


    /**
     * init view
     */
    public void initData(HomeWorkResponse.HomeWorkBean homeWorkBean, boolean isLast) {
        if (homeWorkBean != null) {
            mHomeWorkBean = homeWorkBean;
            mHomeWorkName.setText("作业名称:" + homeWorkBean.getName());
            if (TextUtils.isEmpty(homeWorkBean.getStatus())) {
                mReadState.setVisibility(View.GONE);
            } else if(homeWorkBean.getStatus().equals("0")){
                mReadState.setVisibility(View.VISIBLE);
                mReadState.setImageResource(R.mipmap.icon_unread);
            }else{
                mReadState.setVisibility(View.VISIBLE);
                mReadState.setImageResource(R.mipmap.icon_read);
            }
            if (homeWorkBean.getGallerymediaobjectlist() != null && homeWorkBean.getGallerymediaobjectlist().size() > 0) {
                mHomeWorkTitle.setText("示意素材: ");
                mAttachmentViewer.setVisibility(View.VISIBLE);
                mAttachmentViewer.initData(homeWorkBean.getGallerymediaobjectlist());
            } else {
                mAttachmentViewer.setVisibility(View.GONE);
                mHomeWorkTitle.setText("示意素材: 无");
            }

            if (isLast) {
                mLine.setVisibility(View.GONE);
            } else {
                mLine.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (mHomeWorkBean != null) {
            HomeWorkDetailActivity.startActivity(context, mHomeWorkBean);
        }
    }

}